package cn.com.haopy.yue.ai.core.component;


import cn.com.haopy.yue.ai.core.system.SystemConfig;
import cn.com.haopy.yue.ai.core.utils.RedisCachePreFix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Objects;

@Component
public class SecurityComponent {
    @Autowired
    private CacheComponent cacheComponent;

    public static String CLIENT_TYPE_ADMIN = "admin";

    public void checkPasswordComplexity(String password) {
        if (!StringUtils.hasText(password)) {
            throw new RuntimeException("密码不能为空！");
        }

        Boolean checkComplexity = SystemConfig.getSystemUserPasswordComplexityCheck();
        if (!checkComplexity) {
            return;
        }

        if (password.length() < 6) {
            throw new RuntimeException("密码长度不能小于6位！");
        }

        if (password.matches("[0-9]+")) {
            throw new RuntimeException("密码不能全是数字！");
        }

        if (password.matches("[a-z]+")) {
            throw new RuntimeException("密码不能全是小写字母！");
        }

        if (password.matches("[A-Z]+")) {
            throw new RuntimeException("密码不能全是大写字母！");
        }

        if (password.matches("[^0-9a-zA-Z]+")) {
            throw new RuntimeException("密码不能全是特殊符号！");
        }

        if (password.matches("[0-9a-zA-Z]+")) {
            throw new RuntimeException("密码不能全是字母和数字！");
        }
    }

    public boolean checkPasswordExpired(LocalDateTime lastChangeTime) {
        if (!SystemConfig.getSystemUserPasswordForcedExpiredEnable()) {
            return false;
        }

        if (Objects.isNull(lastChangeTime)) {
            return true;
        }

        int expiredDays = SystemConfig.getSystemUserPasswordForcedExpiredDays();
        if (expiredDays <= 0) {
            return true;
        }

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime expiredTime = lastChangeTime.plusDays(expiredDays);
        return now.isAfter(expiredTime);
    }

    public void checkLock(String userName, String clientType, String ip) throws Exception {
        String key = getKey(userName, clientType, ip);
        boolean isHas = cacheComponent.hasKey(key);
        if (!isHas) {
            return;
        }

        int remain = Integer.parseInt(cacheComponent.getRaw(key));
        if (remain > 0) {
            return;
        }

        // 账户已锁定
        long lockTime = cacheComponent.getExpireSec(key);
//        if (lockTime > 60) {
//            lockTime = lockTime / 60;
//            throw new Exception("账户已锁定，" + lockTime + "分钟后重试！");
//        } else {
            throw new Exception("账户已锁定，" + lockTime + "秒后重试！");
//        }
    }

    public Integer loginFailed(String userName, String clientType, String ip) {
        String key = getKey(userName, clientType, ip);

        Integer remain;

        boolean isHad = cacheComponent.hasKey(key);
        if (isHad) {
            remain = Integer.valueOf(cacheComponent.getRaw(key));
        } else {
            remain = SystemConfig.getSystemUserLoginMaxFailedCount();
        }

        remain--;

        Integer lockTime = SystemConfig.getSystemUserLoginLockTimeout();

        cacheComponent.putRaw(key, String.valueOf(remain), lockTime);

        return remain;
    }

    public void removeLock(String userName, String clientType, String ip) {
        cacheComponent.del(getKey(userName, clientType, ip));
    }

    private String getKey(String username, String clientType, String ip) {
        return RedisCachePreFix.USER_LOGIN_LOCK.getCachePreFix() + clientType + "_" + username;
    }
}
